/* 
 * 厦门大学嘉庚学院图书馆入馆考试自动答题脚本
 * 作者：Mutoo
 * 
 * 更新时间：2011-9-4 凌晨4点
 * 
 * 使用方法：
 * 进入考试系统;
 * 选择你要回答的题型
 * 在地址栏输入以下脚本
 * 
 * 		javascript:(function(){var s=document.createElement("script");s.src="http://swe09018-stu-xujc-com.googlecode.com/svn/trunk/javascript/exam.js"; document.body.appendChild(s)})();
 * 
 * 按回车运行，脚本开始自动回答当前所在题型;
 * 当前类型答题结束后，手动切换到其它题型，并重复以上操作;
 * 全部题型完成后手动交卷。
 * 
 * 题库来源：http://wenku.baidu.com/view/24427c7931b765ce05081456.html
 * 题库日期：2010-10-15
 * 
 * */

// 单选题 http://210.34.218.48/exam/single.aspx
// 多选题 http://210.34.218.48/exam/Multi.aspx
// 判断题 http://210.34.218.48/exam/Judge.aspx

// 单选题库及答案
quiz_single = [['f5bee1580e36758b355c1a6d1c1c15a4',0], ['791a21235a88eb59f7d418fbe284e01e',2], ['a84e867386509db8148e6dca13dbb8fe',1], ['8147e6702ab862f51c9976605e0e8930',0], ['9f7d3d9532c5e416ce0cfd18b4f67fa9',2], ['d50c9192a5b216f947720e232031b21c',3], ['be87b227959dcd2da14eb0ba7b97a0fa',2], ['a2716a17e26bd9415499ef12cd07ca9e',1], ['7d723ba40de26630dfbea2f9ec9e6ac4',2], ['9fde673bfc7d73f70ef6caa9306bfa22',0], ['83fb28f9fd2bf9854e4ccc725b2e8284',1], ['80818e5d0ce41088e22e2c1318dd4b1a',3], ['0248f967e8da80519b87a58fc8904d6f',0], ['fdd9a3d558f30fa7594f0cbc36a7c1f2',1], ['7cf780de96021f45ea6d3ff435807e30',2], ['9d24f94f5ed5041d042e07c6189e6953',2], ['87bdfa3bb9e79860c0c30efcf6a98f25',3], ['18b85b6b8cf282eb8b86ff5d7c176895',0], ['8cfd439e0776344ec06d0ff0b7825fbb',2], ['f6830cc56994edc31d34b9d8059063a2',0], ['a2716a17e26bd9415499ef12cd07ca9e',1], ['791a21235a88eb59f7d418fbe284e01e',2], ['cbc745c5889641a1072f1429d60ba41f',2], ['83fb28f9fd2bf9854e4ccc725b2e8284',1], ['7e2933bfd44ee96152510097834345d1',1], ['80818e5d0ce41088e22e2c1318dd4b1a',3], ['a970602dde9e689e132a2c583728cffb',1], ['7cf780de96021f45ea6d3ff435807e30',2], ['a84e867386509db8148e6dca13dbb8fe',1], ['87bdfa3bb9e79860c0c30efcf6a98f25',3], ['fdd9a3d558f30fa7594f0cbc36a7c1f2',1], ['18b85b6b8cf282eb8b86ff5d7c176895',0], ['8147e6702ab862f51c9976605e0e8930',0], ['3a53e9d0b93768f5fa1a3fd9302222d8',3], ['7d723ba40de26630dfbea2f9ec9e6ac4',2], ['f6830cc56994edc31d34b9d8059063a2',0], ['9d24f94f5ed5041d042e07c6189e6953',2], ['27d03d8b5d2e94f4f902f6d3e87c6e2e',1], ['b48d20773de22fb6d38e3b239838b6a7',3], ['0248f967e8da80519b87a58fc8904d6f',0], ['a84e867386509db8148e6dca13dbb8fe',1], ['686eaf1ba54fb6529062fc3eb3543be4',1], ['a970602dde9e689e132a2c583728cffb',1], ['0248f967e8da80519b87a58fc8904d6f',0], ['f6830cc56994edc31d34b9d8059063a2',0], ['7cf780de96021f45ea6d3ff435807e30',2], ['18b85b6b8cf282eb8b86ff5d7c176895',0], ['a2716a17e26bd9415499ef12cd07ca9e',1], ['be87b227959dcd2da14eb0ba7b97a0fa',2], ['8cfd439e0776344ec06d0ff0b7825fbb',2], ['fdd9a3d558f30fa7594f0cbc36a7c1f2',1], ['9d24f94f5ed5041d042e07c6189e6953',2], ['9f7d3d9532c5e416ce0cfd18b4f67fa9',2], ['7e2933bfd44ee96152510097834345d1',1], ['d50c9192a5b216f947720e232031b21c',3], ['b48d20773de22fb6d38e3b239838b6a7',3], ['80818e5d0ce41088e22e2c1318dd4b1a',3], ['27d03d8b5d2e94f4f902f6d3e87c6e2e',1], ['8147e6702ab862f51c9976605e0e8930',0], ['83fb28f9fd2bf9854e4ccc725b2e8284',1], ['87bdfa3bb9e79860c0c30efcf6a98f25',3], ['a2716a17e26bd9415499ef12cd07ca9e',1], ['cbc745c5889641a1072f1429d60ba41f',2], ['9f7d3d9532c5e416ce0cfd18b4f67fa9',2], ['80818e5d0ce41088e22e2c1318dd4b1a',3], ['9d24f94f5ed5041d042e07c6189e6953',2], ['b48d20773de22fb6d38e3b239838b6a7',3], ['83fb28f9fd2bf9854e4ccc725b2e8284',1], ['7cf780de96021f45ea6d3ff435807e30',2], ['7e2933bfd44ee96152510097834345d1',1], ['f6830cc56994edc31d34b9d8059063a2',0], ['3a53e9d0b93768f5fa1a3fd9302222d8',3], ['d50c9192a5b216f947720e232031b21c',3], ['a970602dde9e689e132a2c583728cffb',1], ['686eaf1ba54fb6529062fc3eb3543be4',1], ['a84e867386509db8148e6dca13dbb8fe',1], ['9fde673bfc7d73f70ef6caa9306bfa22',0], ['8cfd439e0776344ec06d0ff0b7825fbb',2], ['18b85b6b8cf282eb8b86ff5d7c176895',0], ['fdd9a3d558f30fa7594f0cbc36a7c1f2',1], ['a84e867386509db8148e6dca13dbb8fe',1], ['0248f967e8da80519b87a58fc8904d6f',0], ['7cf780de96021f45ea6d3ff435807e30',2], ['be87b227959dcd2da14eb0ba7b97a0fa',2], ['3a53e9d0b93768f5fa1a3fd9302222d8',3], ['f6830cc56994edc31d34b9d8059063a2',0], ['87bdfa3bb9e79860c0c30efcf6a98f25',3], ['9d24f94f5ed5041d042e07c6189e6953',2], ['cbc745c5889641a1072f1429d60ba41f',2], ['686eaf1ba54fb6529062fc3eb3543be4',1], ['791a21235a88eb59f7d418fbe284e01e',2], ['b48d20773de22fb6d38e3b239838b6a7',3], ['8cfd439e0776344ec06d0ff0b7825fbb',2], ['18b85b6b8cf282eb8b86ff5d7c176895',0], ['80818e5d0ce41088e22e2c1318dd4b1a',3], ['9f7d3d9532c5e416ce0cfd18b4f67fa9',2], ['27d03d8b5d2e94f4f902f6d3e87c6e2e',1], ['83fb28f9fd2bf9854e4ccc725b2e8284',1], ['f5bee1580e36758b355c1a6d1c1c15a4',0], ['8147e6702ab862f51c9976605e0e8930',0]];
// 多选题库及答案
quiz_multi = [['91ada282164e59afcb92aee8759c92b9',[1,1,1,1]], ['2cbd998be7beb4d293d147ce7022c2d7',[1,1,0,1]], ['c3fd9e97491ecb5f0ac7d4b91c0fc028',[1,1,1,1]], ['a03c0d6b97a43149cb8acdc0f7132f7f',[1,0,1,0]], ['512c3b9c5fbc8c73d9e81538f96044a5',[1,1,1,0]], ['5838a4909220a0b2d011f0a3e2f3d461',[1,1,1,1]], ['5c6fe1921f60d29156b06c2162fbddea',[0,1,1,1]], ['8a289233d9bdb2f317d8e844105c8b25',[1,1,0,1]], ['511e0669e89dc15de7ef6c335fbd1e50',[1,1,1,0]], ['5f7ab729b561707d96e5fda0e6704e6d',[1,0,1,0]], ['4f73c68700fcbce507c74f844f70aac8',[1,1,1,1]], ['8fe1759853800e2cea1d09918b136658',[1,1,1,0]], ['292757dcfbe75bf1026ca6fcf24ffd08',[1,1,0,0]], ['b2223036f74ed135f3489277d7fab77d',[1,1,1,0]], ['1d3ed7a3468e3aefb90ba88ea34da9fe',[1,1,1,1]], ['9d6dc329bcb82511a2eb0bea63774dc7',[1,1,1,1]], ['d0655360e77643960136e128133c9dfc',[1,1,1,1]], ['ee187ee3e14946aa08bb52f310a6bd11',[1,1,1,1]], ['4f73c68700fcbce507c74f844f70aac8',[1,1,1,1]], ['a1919eda92053e3ee007d0bb0ac6f879',[1,1,1,0]], ['3e663d0ea4104fc7dffecb5a69f516a3',[0,1,1,0]], ['eca3ebe9cfed4f2fe37c9c89143057f1',[1,0,1,0]], ['b786a9088fc17c945c735f2b9280e7d7',[0,1,1,0]], ['65a1b90a22699943b7a328623bf72d2b',[1,1,1,0]], ['0ac66c60b2a705dd5f34ca77a0af488a',[1,0,1,0]], ['42b0a2d58d6d86a11601596b4a0e8308',[1,1,1,1]], ['512c3b9c5fbc8c73d9e81538f96044a5',[1,1,1,0]], ['a6f424ef246d1e1dabf7b8fd4a215993',[1,1,1,1]], ['464c29ff8c1df8f8fd3e44426c122f7f',[0,1,1,1]], ['c3fd9e97491ecb5f0ac7d4b91c0fc028',[1,1,1,1]], ['eca3ebe9cfed4f2fe37c9c89143057f1',[1,0,1,0]], ['63e19867a76d871134ac2ab04cfaadf2',[1,1,1,1]], ['3e663d0ea4104fc7dffecb5a69f516a3',[0,1,1,0]], ['292757dcfbe75bf1026ca6fcf24ffd08',[1,1,0,0]], ['b2223036f74ed135f3489277d7fab77d',[1,1,1,0]], ['2572eb5b20f272357451d142b0f45cdd',[0,1,1,0]], ['0ac66c60b2a705dd5f34ca77a0af488a',[1,0,1,0]], ['8a289233d9bdb2f317d8e844105c8b25',[1,1,0,1]], ['5c6fe1921f60d29156b06c2162fbddea',[0,1,1,1]], ['a24c7b918cff0b59963a86691fc567e7',[1,1,1,1]], ['65a1b90a22699943b7a328623bf72d2b',[1,1,1,0]], ['512c3b9c5fbc8c73d9e81538f96044a5',[1,1,1,0]], ['c3fd9e97491ecb5f0ac7d4b91c0fc028',[1,1,1,1]], ['9d6dc329bcb82511a2eb0bea63774dc7',[1,1,1,1]], ['b654d0aadb1d76a352244f207348b817',[1,1,0,0]], ['b786a9088fc17c945c735f2b9280e7d7',[0,1,1,0]], ['8a289233d9bdb2f317d8e844105c8b25',[1,1,0,1]], ['9d5b620a94312542b245059aef5d2247',[1,1,1,1]], ['1d3ed7a3468e3aefb90ba88ea34da9fe',[1,1,1,1]], ['8fe1759853800e2cea1d09918b136658',[1,1,1,0]], ['a1919eda92053e3ee007d0bb0ac6f879',[1,1,1,0]], ['b654d0aadb1d76a352244f207348b817',[1,1,0,0]], ['a6f424ef246d1e1dabf7b8fd4a215993',[1,1,1,1]], ['b2223036f74ed135f3489277d7fab77d',[1,1,1,0]], ['0ac66c60b2a705dd5f34ca77a0af488a',[1,0,1,0]], ['2572eb5b20f272357451d142b0f45cdd',[0,1,1,0]], ['42b0a2d58d6d86a11601596b4a0e8308',[1,1,1,1]], ['5838a4909220a0b2d011f0a3e2f3d461',[1,1,1,1]], ['3e663d0ea4104fc7dffecb5a69f516a3',[0,1,1,0]], ['63e19867a76d871134ac2ab04cfaadf2',[1,1,1,1]], ['c3fd9e97491ecb5f0ac7d4b91c0fc028',[1,1,1,1]], ['292757dcfbe75bf1026ca6fcf24ffd08',[1,1,0,0]], ['b2223036f74ed135f3489277d7fab77d',[1,1,1,0]], ['5c6fe1921f60d29156b06c2162fbddea',[0,1,1,1]], ['eca3ebe9cfed4f2fe37c9c89143057f1',[1,0,1,0]], ['42b0a2d58d6d86a11601596b4a0e8308',[1,1,1,1]], ['63e19867a76d871134ac2ab04cfaadf2',[1,1,1,1]], ['2cbd998be7beb4d293d147ce7022c2d7',[1,1,0,1]], ['5457a33dfcbf2b1766c36a589c015f90',[1,1,1,0]], ['a1919eda92053e3ee007d0bb0ac6f879',[1,1,1,0]], ['9d5b620a94312542b245059aef5d2247',[1,1,1,1]], ['2572eb5b20f272357451d142b0f45cdd',[0,1,1,0]], ['a03c0d6b97a43149cb8acdc0f7132f7f',[1,0,1,0]], ['1d3ed7a3468e3aefb90ba88ea34da9fe',[1,1,1,1]], ['ee187ee3e14946aa08bb52f310a6bd11',[1,1,1,1]]];
// 判断题库及答案
quiz_judge = [['c6c78a0483ec5dcad0ae9b774270c8a0',1], ['2ef3f73afe04447bdb732982c7f7232d',1], ['9c78749fcd94f343802c2e7b5aa850b9',0], ['53d69cc64a8dfbc43a8b321538fd6104',0], ['809cf82aea4ed90c0765de28b3f857e6',0], ['5ed2ed27ab43bc2f4223adce4cd7a515',1], ['ba29d1b1de1f9890bfe7fe357af19e40',1], ['99260869f07d8c9d241ddfdcbc445aef',0], ['27ceaf287b62ec900816ea50fe8ebaf2',1], ['8e4676d1e8a4a1a0202709f7261750ba',1], ['e13cbe5b47d06641e4bacd7ac99579ad',0], ['18ec4dd9d8b7e1c9786290912deecd6e',1], ['adfe8dbe9f22ab4d74d2545199b6a32f',0], ['588e4da6037b027474d062c6d6f5473f',0], ['ad7f108043a44fed655fe04c39a8e33d',1], ['99260869f07d8c9d241ddfdcbc445aef',0], ['d9f039d4fb010f41b8a6490b4da87371',0], ['ba29d1b1de1f9890bfe7fe357af19e40',1], ['e13cbe5b47d06641e4bacd7ac99579ad',0], ['18ec4dd9d8b7e1c9786290912deecd6e',1], ['5ed2ed27ab43bc2f4223adce4cd7a515',1], ['9c78749fcd94f343802c2e7b5aa850b9',0], ['2ef3f73afe04447bdb732982c7f7232d',1], ['809cf82aea4ed90c0765de28b3f857e6',0], ['ad7f108043a44fed655fe04c39a8e33d',1], ['53d69cc64a8dfbc43a8b321538fd6104',0], ['744f8f24de4a4cac4ba1737979d84019',1], ['8e4676d1e8a4a1a0202709f7261750ba',1], ['27ceaf287b62ec900816ea50fe8ebaf2',1], ['caa6143f7ecc749dde38af60f99c62fc',1], ['9c78749fcd94f343802c2e7b5aa850b9',0], ['e13cbe5b47d06641e4bacd7ac99579ad',0], ['5ed2ed27ab43bc2f4223adce4cd7a515',1], ['adfe8dbe9f22ab4d74d2545199b6a32f',0], ['caa6143f7ecc749dde38af60f99c62fc',1], ['ad7f108043a44fed655fe04c39a8e33d',1], ['99260869f07d8c9d241ddfdcbc445aef',0], ['ba29d1b1de1f9890bfe7fe357af19e40',1], ['d2a48aefe73aa61a694b37129ea31fc1',0], ['809cf82aea4ed90c0765de28b3f857e6',0], ['588e4da6037b027474d062c6d6f5473f',0], ['18ec4dd9d8b7e1c9786290912deecd6e',1], ['744f8f24de4a4cac4ba1737979d84019',1], ['53d69cc64a8dfbc43a8b321538fd6104',0], ['c6c78a0483ec5dcad0ae9b774270c8a0',1], ['5f5f8ea7b71c15d54ce310185fc101da',1] ['2ef3f73afe04447bdb732982c7f7232d',1], ['99260869f07d8c9d241ddfdcbc445aef',0], ['c6c78a0483ec5dcad0ae9b774270c8a0',1], ['18ec4dd9d8b7e1c9786290912deecd6e',1], ['588e4da6037b027474d062c6d6f5473f',0], ['744f8f24de4a4cac4ba1737979d84019',1], ['adfe8dbe9f22ab4d74d2545199b6a32f',0], ['ad7f108043a44fed655fe04c39a8e33d',1], ['caa6143f7ecc749dde38af60f99c62fc',1], ['5ed2ed27ab43bc2f4223adce4cd7a515',1], ['9c78749fcd94f343802c2e7b5aa850b9',0], ['8e4676d1e8a4a1a0202709f7261750ba',1], ['d2a48aefe73aa61a694b37129ea31fc1',0], ['27ceaf287b62ec900816ea50fe8ebaf2',1], ['53d69cc64a8dfbc43a8b321538fd6104',0], ['9c78749fcd94f343802c2e7b5aa850b9',0], ['adfe8dbe9f22ab4d74d2545199b6a32f',0], ['ba29d1b1de1f9890bfe7fe357af19e40',1], ['53d69cc64a8dfbc43a8b321538fd6104',0], ['caa6143f7ecc749dde38af60f99c62fc',1], ['2ef3f73afe04447bdb732982c7f7232d',1], ['d2a48aefe73aa61a694b37129ea31fc1',0], ['c6c78a0483ec5dcad0ae9b774270c8a0',1], ['18ec4dd9d8b7e1c9786290912deecd6e',1], ['e13cbe5b47d06641e4bacd7ac99579ad',0], ['d9f039d4fb010f41b8a6490b4da87371',0], ['8e4676d1e8a4a1a0202709f7261750ba',1], ['809cf82aea4ed90c0765de28b3f857e6',0], ['744f8f24de4a4cac4ba1737979d84019',1], ['ad7f108043a44fed655fe04c39a8e33d',1]];

/* 判断题型 */
function Answer(){
	
	var url = location.href.split('/');
	var page = url[url.length-1];
	var type = page.split('.')[0].toLowerCase();
	
	switch(type){
	case 'single':
		Single();
		break;
	case 'multi':
		Multi();
		break;
	case 'judge':
		Judge();
		break;
	}
	
}

/* 回答单选题 */
function Single(){
	
	quiz = document.getElementById("Title1");
	quiz_title = hex_md5(quiz.innerHTML);
	
	choice_A = document.getElementById("RadioBtA1");
	choice_B = document.getElementById("RadioBtB1");
	choice_C = document.getElementById("RadioBtC1");
	choice_D = document.getElementById("RadioBtD1");
	choice = [choice_A, choice_B, choice_C, choice_D];
	
	var flag = false;
	
	for(var i=0; i<quiz_single.length; i++){
		if(quiz_title == quiz_single[i][0]){
			flag = true;
			choice[quiz_single[i][1]].click();
			break;
		}
	}
	
	if(!flag){
		//alert("该题没在题库中，随机答题 :)");
		choice[Math.floor(Math.random()*4)].click();
	}
	
	nextBtn = document.getElementById("NextQuestion");
	
	setTimeout(function(){
		if(!nextBtn.disabled){
			nextBtn.click(); 
			setTimeout('Single()', 500);
		}else{
			alert("当前题型已答完 :)");
		}
	}, 500);
	
}

/* 回答多选题 */
function Multi(){
	
	quiz = document.getElementById("Title1");
	quiz_title = hex_md5(quiz.innerHTML);
	
	var flag = false;
	for(var i=0; i<quiz_multi.length; i++){
		if(quiz_title == quiz_multi[i][0]){
			flag = true;
			//alert(quiz_multi[i][1]);
			for(var j=0; j<4; j++){
				setTimeout('doMulti('+i+','+j+')', j*500);
			}
			break;
		}
	}
	
	if(!flag){
		choice_A = document.getElementById("CheckBoxA");
		choice_B = document.getElementById("CheckBoxB");
		choice_C = document.getElementById("CheckBoxC");
		choice_D = document.getElementById("CheckBoxD");
		choice = [choice_A, choice_B, choice_C, choice_D];
		//alert("该题没在题库中，随机答题 :)");
		choice[Math.floor(Math.random()*4)].click();
	}
	
	if(!j) j=5;
	
	setTimeout(function(){
		nextBtn = document.getElementById("NextQuestion");
		if(!nextBtn.disabled){
			nextBtn.click(); 
			setTimeout('Multi()', 500);
		}else{
			alert("当前题型已答完 :)");
		}
	}, j*500);
	
}
function doMulti(i,j){
	choice_A = document.getElementById("CheckBoxA");
	choice_B = document.getElementById("CheckBoxB");
	choice_C = document.getElementById("CheckBoxC");
	choice_D = document.getElementById("CheckBoxD");
	choice = [choice_A, choice_B, choice_C, choice_D];
	
	if(choice[j].checked!=quiz_multi[i][1][j])
		choice[j].click();
}

/* 回答判断题 */
function Judge(){
	
	quiz = document.getElementById("Title1");
	quiz_title = hex_md5(quiz.innerHTML);
	
	choice_A = document.getElementById("CheckBoxA");
	choice_B = document.getElementById("CheckBoxB");
	choice = [choice_A, choice_B];
	
	var flag = false;
	
	for(var i=0; i<quiz_judge.length; i++){
		if(quiz_title == quiz_judge[i][0]){
			flag = true;
			choice[quiz_judge[i][1]].click();
			break;
		}
	}
	
	if(!flag){
		//alert("该题没在题库中，随机答题 :)");
		choice[Math.floor(Math.random()*2)].click();
	}
	
	nextBtn = document.getElementById("NextQuestion");
	
	setTimeout(function(){
		if(!nextBtn.disabled){
			nextBtn.click(); 
			setTimeout('Judge()', 500);
		}else{
			alert("当前题型已答完 :)");
		}
	}, 500);
	
}

Answer(); // 开始答题

/* MD5生成函数: hex_md5(str) */

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = "";  /* base-64 pad character. "=" for strict RFC compliance   */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s)    { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
function b64_md5(s)    { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
function hex_hmac_md5(k, d)
  { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
function b64_hmac_md5(k, d)
  { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
function any_hmac_md5(k, d, e)
  { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
  return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of a raw string
 */
function rstr_md5(s)
{
  return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
}

/*
 * Calculate the HMAC-MD5, of a key and some data (raw strings)
 */
function rstr_hmac_md5(key, data)
{
  var bkey = rstr2binl(key);
  if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
}

/*
 * Convert a raw string to a hex string
 */
function rstr2hex(input)
{
  try { hexcase } catch(e) { hexcase=0; }
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var output = "";
  var x;
  for(var i = 0; i < input.length; i++)
  {
    x = input.charCodeAt(i);
    output += hex_tab.charAt((x >>> 4) & 0x0F)
           +  hex_tab.charAt( x        & 0x0F);
  }
  return output;
}

/*
 * Convert a raw string to a base-64 string
 */
function rstr2b64(input)
{
  try { b64pad } catch(e) { b64pad=''; }
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var output = "";
  var len = input.length;
  for(var i = 0; i < len; i += 3)
  {
    var triplet = (input.charCodeAt(i) << 16)
                | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
                | (i + 2 < len ? input.charCodeAt(i+2)      : 0);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > input.length * 8) output += b64pad;
      else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
    }
  }
  return output;
}

/*
 * Convert a raw string to an arbitrary string encoding
 */
function rstr2any(input, encoding)
{
  var divisor = encoding.length;
  var i, j, q, x, quotient;

  /* Convert to an array of 16-bit big-endian values, forming the dividend */
  var dividend = Array(Math.ceil(input.length / 2));
  for(i = 0; i < dividend.length; i++)
  {
    dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  }

  /*
   * Repeatedly perform a long division. The binary array forms the dividend,
   * the length of the encoding is the divisor. Once computed, the quotient
   * forms the dividend for the next step. All remainders are stored for later
   * use.
   */
  var full_length = Math.ceil(input.length * 8 /
                                    (Math.log(encoding.length) / Math.log(2)));
  var remainders = Array(full_length);
  for(j = 0; j < full_length; j++)
  {
    quotient = Array();
    x = 0;
    for(i = 0; i < dividend.length; i++)
    {
      x = (x << 16) + dividend[i];
      q = Math.floor(x / divisor);
      x -= q * divisor;
      if(quotient.length > 0 || q > 0)
        quotient[quotient.length] = q;
    }
    remainders[j] = x;
    dividend = quotient;
  }

  /* Convert the remainders to the output string */
  var output = "";
  for(i = remainders.length - 1; i >= 0; i--)
    output += encoding.charAt(remainders[i]);

  return output;
}

/*
 * Encode a string as utf-8.
 * For efficiency, this assumes the input is valid utf-16.
 */
function str2rstr_utf8(input)
{
  var output = "";
  var i = -1;
  var x, y;

  while(++i < input.length)
  {
    /* Decode utf-16 surrogate pairs */
    x = input.charCodeAt(i);
    y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
    if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
    {
      x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
      i++;
    }

    /* Encode output as utf-8 */
    if(x <= 0x7F)
      output += String.fromCharCode(x);
    else if(x <= 0x7FF)
      output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
                                    0x80 | ( x         & 0x3F));
    else if(x <= 0xFFFF)
      output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
                                    0x80 | ((x >>> 6 ) & 0x3F),
                                    0x80 | ( x         & 0x3F));
    else if(x <= 0x1FFFFF)
      output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
                                    0x80 | ((x >>> 12) & 0x3F),
                                    0x80 | ((x >>> 6 ) & 0x3F),
                                    0x80 | ( x         & 0x3F));
  }
  return output;
}

/*
 * Encode a string as utf-16
 */
function str2rstr_utf16le(input)
{
  var output = "";
  for(var i = 0; i < input.length; i++)
    output += String.fromCharCode( input.charCodeAt(i)        & 0xFF,
                                  (input.charCodeAt(i) >>> 8) & 0xFF);
  return output;
}

function str2rstr_utf16be(input)
{
  var output = "";
  for(var i = 0; i < input.length; i++)
    output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
                                   input.charCodeAt(i)        & 0xFF);
  return output;
}

/*
 * Convert a raw string to an array of little-endian words
 * Characters >255 have their high-byte silently ignored.
 */
function rstr2binl(input)
{
  var output = Array(input.length >> 2);
  for(var i = 0; i < output.length; i++)
    output[i] = 0;
  for(var i = 0; i < input.length * 8; i += 8)
    output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  return output;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2rstr(input)
{
  var output = "";
  for(var i = 0; i < input.length * 32; i += 8)
    output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  return output;
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length.
 */
function binl_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);
}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}